﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using KPIS.GERP.GAMS.MODEL;
using KPIS.GERP.GAMS.DAL;
using Telerik.Web.UI;
using System.Collections.Generic;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_MAS_Account_Version : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_ACM_Account_Version d_mas_acc_ver = new D_ACM_Account_Version();
        M_ACM_Account_Version m_mas_acc_ver = new M_ACM_Account_Version();
        M_ACM_MAS_Account m_acm_mas_account = new M_ACM_MAS_Account();
        D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();
        //static IList<M_ACM_Account_Version> ProjectPerInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load");
               // Session["UserID"] = "555";
                //RadFileExplorer1.Configuration.ContentProviderTypeName = typeof(Telerik.Web.TelerikAnimations).AssemblyQualifiedName;
                //RadFileExplorer1.Configuration.ViewPaths = new string[] { "ROOT/Images/Nature", "ROOT/Images/People" };
            }
        }

        //============================ control events ==============================

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            UpdateUI1("Insert");
            EnableObj(true);
            ClearObj();
            lblCreateBy.Text = main.strEmpID;
            lblCreateDate.Text = DateTime.Now.ToLongDateString();
            txtVersionNo.Focus();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            txtVersionNo.Focus();
            UpdateUI1("Edit");
            EnableObj(true);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            UpdateUI1("Delete");
            EnableObj(false);
            m_mas_acc_ver.version_seq = Convert.ToInt32(hSeq.Text);
            //m_mas_acc_ver.version_code = txtVersionNo.Text;
            //m_mas_acc_ver.version_descr = txtVessionDescr.Text;
            //m_mas_acc_ver.record_status = true;//chkActiveFlag.Checked;
            m_mas_acc_ver.updated_by = Convert.ToInt32(main.strEmpID);
            m_mas_acc_ver.updated_when = DateTime.Now;
            d_mas_acc_ver.Update(m_mas_acc_ver);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateObj() == true)
            {
                if (hStatus.Text == "Insert")
                {
                    try
                    {
                        if (!chkActiveFlag.Checked)
                        {
                            var model = d_mas_acc_ver.GetList(true, null);

                            if (model.Count > 0)
                            {
                                lblError.Text = "ไม่สามารถใช้งานผังบัญชีหลาย version พร้อมกันได้ กรุณายกเลิกผังบัญชี Version อื่นๆ ก่อน";
                                return;
                            }
                        }

                        lblError.Text = "";
                        m_mas_acc_ver.version_code = txtVersionNo.Text;
                        m_mas_acc_ver.version_descr = txtVessionDescr.Text;
                        m_mas_acc_ver.record_status = "U";//chkActiveFlag.Checked;
                        m_mas_acc_ver.created_by = Convert.ToInt32(main.strEmpID);
                        m_mas_acc_ver.created_when = DateTime.Now;
                        m_mas_acc_ver.use_flag = !chkActiveFlag.Checked;
                        d_mas_acc_ver.Insert(m_mas_acc_ver);

                        InsertAccountNew("ทรัพย์สิน");
                        InsertAccountNew("หนี้สิน");
                        InsertAccountNew("ทุน");
                        InsertAccountNew("รายได้");
                        InsertAccountNew("ค่าใช้จ่าย");
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }

                }
                else if (hStatus.Text == "Update")
                {
                    if (!chkActiveFlag.Checked)
                    {
                        var model = d_mas_acc_ver.GetList(true, Convert.ToInt32(hSeq.Text));

                        if (model.Count > 0)
                        {
                            lblError.Text = "ไม่สามารถใช้งานผังบัญชีหลาย version พร้อมกันได้ กรุณายกเลิกผังบัญชี Version อื่นๆ ก่อน";
                            return;
                        }
                    }

                    m_mas_acc_ver.version_seq = Convert.ToInt32(hSeq.Text);
                    m_mas_acc_ver.version_code = txtVersionNo.Text;
                    m_mas_acc_ver.version_descr = txtVessionDescr.Text;
                    m_mas_acc_ver.record_status = "U";//chkActiveFlag.Checked;
                    m_mas_acc_ver.updated_by = Convert.ToInt32(main.strEmpID);
                    m_mas_acc_ver.updated_when = DateTime.Now;
                    m_mas_acc_ver.use_flag = !chkActiveFlag.Checked;
                    d_mas_acc_ver.Update(m_mas_acc_ver);
                }

                RadGridVersion.DataSource = d_mas_acc_ver.GetList();
                RadGridVersion.DataBind();
                UpdateUI1("Save");
                EnableObj(false);
            }
            else
            {
                btnSave.Enabled = true;
            }
        }
        protected void InsertAccountNew(string pAccountName)
        {
            m_mas_acc_ver = d_mas_acc_ver.GetMaxSeq();
            m_acm_mas_account.account_code = "";
            m_acm_mas_account.account_name = pAccountName;
            m_acm_mas_account.record_status = "U";
            m_acm_mas_account.version_seq = m_mas_acc_ver.version_seq.ToString();
            m_acm_mas_account.created_by = Convert.ToInt32(main.strEmpID);
            m_acm_mas_account.created_when = DateTime.Now;
            d_acm_mas_account.Insert(m_acm_mas_account);
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
            ClearObj();
            EnableObj(false);
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            UpdateUI1("Search");
        }
        protected void grdACMVersion_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                UpdateUI1("Delete");
                
                string strVersionSeq = RadGridVersion.Items[e.Item.ItemIndex]["version_seq"].Text;
                M_ACM_Account_Version infoList = new M_ACM_Account_Version();
                if (strVersionSeq.Length > 0)
                {
                    EnableObj(false);
                    hSeq.Text = Convert.ToInt32(strVersionSeq).ToString();
                    m_mas_acc_ver.version_seq = Convert.ToInt32(strVersionSeq);
                    m_mas_acc_ver.version_code = txtVersionNo.Text;
                    m_mas_acc_ver.version_descr = txtVessionDescr.Text;
                    //m_mas_acc_ver.record_status = chkActiveFlag.Checked;
                    m_mas_acc_ver.updated_by = Convert.ToInt32(main.strEmpID);
                    m_mas_acc_ver.updated_when = DateTime.Now;
                    d_mas_acc_ver.Delete(m_mas_acc_ver);
                }

                RadGridVersion.DataSource = d_mas_acc_ver.GetList();
                RadGridVersion.DataBind();
                ClearObj();
            }
            else if (e.CommandName == "DataEdit")
            {
                UpdateUI1("Update");
                string strVersionSeq = RadGridVersion.Items[e.Item.ItemIndex]["version_seq"].Text;

                if (strVersionSeq.Length > 0)
                {
                    EnableObj(true);
                    hSeq.Text = Convert.ToInt32(strVersionSeq).ToString();
                    m_mas_acc_ver = d_mas_acc_ver.GetListFillBySeq(Convert.ToInt32(strVersionSeq));
                    txtVersionNo.Text = m_mas_acc_ver.version_code;
                    txtVessionDescr.Text = m_mas_acc_ver.version_descr;
                    lblCreateBy.Text = m_mas_acc_ver.created_by.ToString();
                    lblCreateDate.Text = Convert.ToString(m_mas_acc_ver.created_when);
                    //if (m_mas_acc_ver.use_flag = true)
                    //    chkActiveFlag.Checked = false;
                    //else
                    //    chkActiveFlag.Checked = true;
                    chkActiveFlag.Checked = !m_mas_acc_ver.use_flag;
                }

                RadGridVersion.DataSource = d_mas_acc_ver.GetList();
                RadGridVersion.DataBind();
            }
            else if (e.CommandName == "AddAccount")
            {
                string strVersionSeq = RadGridVersion.Items[e.Item.ItemIndex]["version_seq"].Text;
                hSeq.Text = Convert.ToInt32(strVersionSeq).ToString();
                Session["VersionSeq"] = hSeq.Text;
                Response.Redirect("ACM_MAS_ACCOUNT_Code.aspx");
            }
            //else if (e.CommandName == "DataEdit")
            //{
            //    lblIndex.Text = e.Item.ItemIndex.ToString();
            //    rblType.SelectedValue = (ProjectPerInfo[e.Item.ItemIndex].version_seq.ToString().Length > 0 ? "1" : "2");
            //    rblType_SelectedIndexChanged(null, null);

            //    if (rblType.SelectedValue == "1")
            //    {
            //        DropDownList_Emp1.SetSelectedValue(ProjectPerInfo[e.Item.ItemIndex].version_seq.ToString());
            //        lblEmpPosName.Text = ProjectPerInfo[e.Item.ItemIndex].per_pos_name;
            //        lblJobsiteName.Text = ProjectPerInfo[e.Item.ItemIndex].per_job_site_name;
            //    }
            //    else
            //    {
            //        DropDownList_Contact1.SetSelectedValue(ProjectPerInfo[e.Item.ItemIndex].contact_seq.ToString());
            //        DropDownList_ContactPerson1.SetSelectedValue(ProjectPerInfo[e.Item.ItemIndex].contact_person_seq.ToString());
            //    }
            //}
        }
        //============================ control events ==============================
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = true;
                    btnCancel.Enabled = false;
                    chkActiveFlag.Enabled = false;
                    break;
                case "Insert":
                    hStatus.Text = "Insert";
                    lblStatus.Text = "เพิ่มข้อมูล";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    chkActiveFlag.Enabled = true;
                    break;
                case "Update":
                    hStatus.Text = "Update";
                    lblStatus.Text = "แก้ไขข้อมูล";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    chkActiveFlag.Enabled = true;
                    break;
                case "Delete":
                    lblStatus.Text = "ลบข้อมูล";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = true;
                    chkActiveFlag.Enabled = false;
                    break;
                case "Save":
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    chkActiveFlag.Enabled = false;
                    break;
                case "Cancel":
                    hStatus.Text = "Cancel";
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    chkActiveFlag.Enabled = false;
                    break;
                case "Search":
                    lblStatus.Text = "ค้นหาข้อมูล";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = true;
                    break;
            }
        }

        //============================ Control Object ==============================
        protected void EnableObj(Boolean pFlag)
        {
            txtVersionNo.Enabled = pFlag;
            txtVessionDescr.Enabled = pFlag;
            // chkActiveFlag.Enabled = pFlag;
        }
        protected void ClearObj()
        {
            txtVersionNo.Text = "";
            txtVessionDescr.Text = "";
            lblCreateBy.Text = "";
            lblCreateDate.Text = "";
        }
        protected string WebString(string String)
        {
            string strResult = "";

            strResult = String.Replace("&nbsp;", "");

            return strResult;
        }
        //============================ Control Object ==============================

        protected void grdACMVersion_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                RadGridVersion.DataSource = d_mas_acc_ver.GetList();
            }
        }
        protected bool ValidateObj()
        {
            bool bFlag = true;
            if (txtVersionNo.Text == "")
            {
                imgWarnVersion.Visible = true;
                lblStatus.Text = "กรุณากรอกหมายเลขเวอร์ชั่นด้วย";
                txtVersionNo.Focus();
                bFlag = false;
            }
            if (txtVersionNo.Text.Length > 10)
            {
                imgWarnVersion.Visible = true;
                lblStatus.Text = "หมายเลขเวอร์ชั่นเกิน 10 ตัว";
                txtVersionNo.Focus();
                bFlag = false;
            }
            if (txtVessionDescr.Text.Length > 77)
            {
                lblStatus.Text = "คำอธิบายของเวอร์ชั่นเกิน 77 อักษร";
                txtVessionDescr.Focus();
                bFlag = false;
            }
            return bFlag;
        }
    }
}
